// Learn TypeScript:
//  - https://docs.cocos.com/creator/manual/en/scripting/typescript.html
// Learn Attribute:
//  - https://docs.cocos.com/creator/manual/en/scripting/reference/attributes.html
// Learn life-cycle callbacks:
//  - https://docs.cocos.com/creator/manual/en/scripting/life-cycle-callbacks.html

import { game } from "../../../../script/game/control/game";
import { ccs } from "../../../../script/util/services";
import { TouchExtend } from "../../../../script/util/TouchExd";
import { touchExd } from "../../../../script/util/TouchExtend";
import { WinBase } from "../../../../script/wins/WinBase";
import achieveItem from "../achieve/achieveItem";



const {ccclass, property} = cc._decorator;

@ccclass
export default class NewClass extends WinBase {

    @property(cc.Node)
    tree: cc.Node = null;

    @property(cc.Prefab) 
    achievePrefab: cc.Prefab = null;

    @property(cc.Node) 
    achieveItem: cc.Node = null;
    achieveList = game.playerMgr.heroInfo.achieve;

    levelList = [];

    freeNodes = [];
    start () {
        this.registerEvent("reAchieve", () => {
            this.show();
        })
        ccs.gf.extendNodeName(this.nodes, this.node.parent);
        let treeList = this.tree.children;
        treeList.splice(8, 1);
        //获取所有的等级位置
        treeList.forEach(val => {
            this.levelList.push({start: val.position.x - val.width/2, end: val.position.x + val.width/2, high: val.position.y});
        })
        this.show();
        
    }
    
    show() {
        let item = null;
        let x = 0, y = 0, pre = null;
        this.achieveList.forEach((val, index) => {
            pre = this.getItem(index);
            if(!pre) {
                pre = cc.instantiate(this.achievePrefab);
                this.tree.addChild(pre);
            }
            //生成圆形 位置等于随机上下最高55点
            item = this.levelList[val.level - 1];

            if(val.cur >= val.target) {
                pre.getComponent(achieveItem).setColor(new cc.Color(132,181,135));
            }
            x = ccs.gf.randomInt(item.start,item.end), y = ccs.gf.randomInt(item.high - 25,item.high + 25);

            pre.setPosition(x, y);
            pre.getComponent(achieveItem).setSize((val.level >= 6? 5: 8 - val.level)/10);
            pre.getComponent(achieveItem).setText(val.name);
            TouchExtend.touchEvent(pre, cc.Node.EventType.TOUCH_END, () => {
                //打开提示
                this.nodes.tips.active = true;
                this.nodes.note.getComponent(cc.Label).string = val.desc;
                this.nodes.title.getComponent(cc.Label).string = val.name;
                if(val.cur >= val.target) {
                    this.nodes.success.getComponent(cc.Label).string = `已达成，达成年龄${val.age}`;
                    this.nodes.success.color = new cc.Color(47,96,101);
                } else {
                    this.nodes.success.getComponent(cc.Label).string = `未达成，当前${ccs.gf.getOmitNum(val.cur)}/${ccs.gf.getOmitNum(val.target)}`;
                    this.nodes.success.color = cc.Color.RED;
                }
            })
            let touch = TouchExtend.touchEvent(this.nodes.tree, cc.Node.EventType.TOUCH_END, () => {
                this.nodes.tips.active = false;
            })
            touch.isScaleEffect = false;
            this.achieveItem = null;
            
        })
        this.onLoadEnd();
    }
    getItem(index) {
        index = index + 8;
        if(this.tree.children[index] ){
            return this.tree.children[index];
        }
        if(this.freeNodes.length > 0 ){
            return this.freeNodes.pop();
        }
        
    }
    onLoadEnd() {
        let childCount = this.tree.children.length - 8;
        let child = null;
        while(this.achieveList.length < childCount) {
            child = this.tree.children[this.achieveList.length];
            child.removeFromParent(false);
            this.freeNodes.push(child);
            childCount --;
        }
    }
    // update (dt) {}
}
